﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using Domain.DAL;
using Casanova.Web;
using WebApp.Reports.Bussiness;
using Utilities;

namespace WebApp.Reports
{
    public partial class ShowReport : System.Web.UI.Page
    {
        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string reportType = QueryStringUtility.GetString("ReportType", string.Empty);
                switch (reportType)
                {
                    case "RepairingTicketList":
                        ShowRepairingTicketList();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        #endregion

        #region Methods

        private void Show(DataSet source, string reportFilePath)
        {
            try
            {
                if (source != null)
                {
                    ReportDocument reportDoc = new ReportDocument();
                    reportDoc.Load(reportFilePath);
                    reportDoc.SetDataSource(source);

                    crwReportViewer.HasToggleGroupTreeButton = false;
                    crwReportViewer.DisplayGroupTree = false;
                    crwReportViewer.BestFitPage = true;
                    crwReportViewer.ReportSource = reportDoc;
                    crwReportViewer.DataBind();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void ShowRepairingTicketList()
        {
            try
            {
                string repairingTicketHistoryIDs = QueryStringUtility.GetString("IDs", string.Empty);
                if (!string.IsNullOrEmpty(repairingTicketHistoryIDs))
                {
                    DataSet source = RepairingTicketListBUS.GetDataSource(repairingTicketHistoryIDs);
                    string reportFilePath = this.ResolveUrl("~/Reports/Templates/RepairingTicketList.rpt");

                    if (source != null && source.Tables.Count > 0 && source.Tables[0].Rows.Count > 0)
                    {
                        Show(source, reportFilePath);
                    }
                    else
                    {
                        Utility.ShowMessageBox(this.Page, Resources.Strings.NoData);
                    }
                }
                else
                {
                    Utility.ShowMessageBox(this.Page, Resources.Strings.NoData);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }
}